/*
 * casper-daemon.h
 *
 *  Created on: 17/feb/2011
 *      Author: ivano
 */

#ifndef CASPERDAEMON_H_
#define CASPERDAEMON_H_

#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif

#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif

#define WRITEMSG(x) x, strlen(x)

#define TERMINALS_BUFFER_SIZE 2048

typedef union {
	struct {
		int to_daemon[2];
		int from_daemon[2];
	} pipes;
	struct {
		/**
		 * Reserved: Daemon only can read from this pin
		 */
		int daemon_read;
		/**
		 * Terminal must use this pin to write
		 */
		int terminal_write;
		/**
		 * Terminal must use this pin to read
		 */
		int terminal_read;
		/**
		 * Reserved: Daemon only can write to this pin
		 */
		int daemon_write;
		/**
		 * Read buffer
		 */
		int _len_terminal;
		int _len_daemon;
		char _buffer_terminal[TERMINALS_BUFFER_SIZE];
		char _buffer_daemon[TERMINALS_BUFFER_SIZE];
	} pins;
} terminal_t;

terminal_t* terminal_new(void);
void terminal_free(terminal_t* term);
int terminal_write_to_daemon(terminal_t* term, char* string);
int terminal_write_to_terminal(terminal_t* term, char* string);
char* terminal_read_from_daemon(terminal_t* term);
char* terminal_read_from_terminal(terminal_t* term);
int terminal_has_more_for_terminal(terminal_t* term);
int terminal_has_more_for_daemon(terminal_t* term);

#endif /* CASPERDAEMON_H_ */
